{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b5d3d2fd-7a1a-415f-b1b6-4da023e9946a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "from matplotlib import gridspec\n",
    "import cartopy.crs as ccrs\n",
    "import cartopy.feature as cfeature\n",
    "import numpy as np\n",
    "import cartopy.io.shapereader as shpreader\n",
    "from netCDF4 import Dataset\n",
    "from shapely.geometry import shape\n",
    "from shapely.geometry import Polygon"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27c9dd63-6b87-4607-94cc-1135407ea829",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1881f91a-201d-4608-ac10-1cf826a9e086",
   "metadata": {},
   "outputs": [],
   "source": [
    "shapefile_path =\"D:/zjaws/330000000.shp\"\n",
    "nc_path = \"D:/Ruler.20200107010000.000.T.nc\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc61eda5-3e67-4c19-8d76-4ea773286be8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# reader = shpreader.Reader(shapefile_path)\n",
    "# min_lon, min_lat, max_lon, max_lat = None, None, None, None\n",
    "# geometries = [record.geometry for record in reader.records()]\n",
    "# polygon = next(reader.geometries())\n",
    "# min_lat = float('inf')\n",
    "# max_lat = float('-inf')\n",
    "# min_lon = float('inf')\n",
    "# max_lon = float('-inf')\n",
    "\n",
    "# for shape_record in reader.records():\n",
    "#     bbox = shape_record.bounds\n",
    "#     min_lon = min(min_lon, bbox[0])\n",
    "#     max_lon = max(max_lon, bbox[2])\n",
    "#     min_lat = min(min_lat, bbox[1])\n",
    "#     max_lat = max(max_lat, bbox[3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "644ba9ff-45cb-42a4-87b8-e06b6b8586b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# max_lon"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6070f91c-e83d-4b4d-af7c-6bf4c46d0f76",
   "metadata": {},
   "outputs": [],
   "source": [
    "# with Dataset(nc_path) as data:\n",
    "#     lon = data.variables['lon'][:]\n",
    "#     lat =  data.variables['lat'][:]\n",
    "#     data =  data.variables['T'][:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ea6bdee-847d-4657-a08f-b7eb0d4a640e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# mask = np.zeros(data.shape, dtype=bool)\n",
    "# for i, n in enumerate(lon):\n",
    "#     for j, t in enumerate(lat):\n",
    "#         point = shape({'type': 'Point', 'coordinates': (n, t)})\n",
    "#         mask[j, i] = polygon.contains(point)\n",
    "# clipped = np.where(mask, data, np.nan)\n",
    "# fig = plt.figure(figsize=(8,8))\n",
    "# gs = gridspec.GridSpec(nrows=1, ncols=2, width_ratios=[1, 0.05], wspace=0.0)\n",
    "# gs = gridspec.GridSpec(nrows=1, ncols=2, width_ratios=[20, 1], height_ratios=[1])\n",
    "# print(mask)\n",
    "# ax = fig.add_subplot(gs[0], projection=ccrs.PlateCarree()) \n",
    "# # ax.set_extent([min_lon, max_lon, min_lat, max_lat], crs=ccrs.PlateCarree())\n",
    "# ax.add_geometries(geometries, ccrs.PlateCarree(), edgecolor='black', facecolor='none')\n",
    "# contour = ax.contourf(lon, lat, clipped, transform=ccrs.PlateCarree())\n",
    "# cbar_ax = fig.add_subplot(gs[1])\n",
    "# cbar = fig.colorbar(contour, ax=cbar_ax, orientation='vertical',fraction=1)\n",
    "# cbar.set_label('Temperature')  # 设置色标标签\n",
    "# print(fig.dpi)\n",
    "# plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e1b1d044-31f8-4dd4-8a55-82f330c3b1b8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b7b44a3d-d15b-4ead-91ba-ed042446f93b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42bddbc7-d74d-4aac-9cbf-c88a85afcb3d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "859490a2-1105-4e66-8aa5-d710392c9ce1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1996b244-e663-42ac-a2c8-71ab617db729",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e0e32946-03ba-4b0a-b2a9-48a0e1629316",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
